package com.next

import com.aliyuncs.DefaultAcsClient
import com.aliyuncs.IAcsClient
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse
import com.aliyuncs.exceptions.ClientException
import com.aliyuncs.profile.DefaultProfile
import com.aliyuncs.profile.IClientProfile
import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

import java.text.SimpleDateFormat

@Transactional
@CompileStatic
@TypeChecked
class SendSMSService
{
    private static final String product = "Dysmsapi"
    private static final String domain = "dysmsapi.aliyuncs.com"
    private static final String accessKeyId = "LTAI2wBYup9b6piE"
    private static final String accessKeySecret = "PGkJ9B4Uxn3oSptlPT0jlrBVe0ysEU"

    SendSmsResponse sendSms(String phoneNum, String signName, String templateCode, String json) throws ClientException
    {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000")
        System.setProperty("sun.net.client.defaultReadTimeout", "10000")
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI2wBYup9b6piE", "PGkJ9B4Uxn3oSptlPT0jlrBVe0ysEU")
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com")
        IAcsClient acsClient = new DefaultAcsClient(profile)
        SendSmsRequest request = new SendSmsRequest()
        request.setPhoneNumbers(phoneNum)
        request.setSignName(signName)
        request.setTemplateCode(templateCode)
        request.setTemplateParam(json)
        SendSmsResponse sendSmsResponse = (SendSmsResponse) acsClient.getAcsResponse(request)
        return sendSmsResponse
    }

    QuerySendDetailsResponse querySendDetails(String phoneNum, Date date, Long pageSize, Long pageNo) throws ClientException
    {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000")
        System.setProperty("sun.net.client.defaultReadTimeout", "10000")
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI2wBYup9b6piE", "PGkJ9B4Uxn3oSptlPT0jlrBVe0ysEU")
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com")
        IAcsClient acsClient = new DefaultAcsClient(profile)
        QuerySendDetailsRequest request = new QuerySendDetailsRequest()
        request.setPhoneNumber(phoneNum)
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd")
        request.setSendDate(ft.format(date))
        request.setPageSize(pageSize != null && pageSize != 0L ? pageSize : 1000L)
        request.setCurrentPage(pageNo != null && pageNo != 0L ? pageNo : 1L)
        QuerySendDetailsResponse querySendDetailsResponse = (QuerySendDetailsResponse) acsClient.getAcsResponse(request)
        return querySendDetailsResponse
    }
}
